//
//  MoveCalloutView.m
//  CarLifes
//
//  Created by yu on 2017/7/12.
//  Copyright © 2017年 jpas. All rights reserved.
//

#define kArrorHeight        10

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50

#define kTitleWidth         120
#define kTitleHeight        20

#define kCalloutInfoCellId @"CalloutInfoCellId"

#import "MoveCalloutView.h"
#import "CalloutInfoCell.h"
#import "TraceModel.h"
#import "Utility.h"

@interface MoveCalloutView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) TraceModel *traceModel;

@property (nonatomic, copy  ) NSString *ownerName;

@property (nonatomic, copy  ) NSString *cnStatus;

@property (nonatomic, assign) DisplayType displayType;

@end

@implementation MoveCalloutView

- (instancetype)initWithType:(DisplayType)type
{
    if (self = [super init]) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame Type:(DisplayType)type
{
    if (self = [super initWithFrame:frame]) {
        self.displayType = type;
        [self.tableView registerNib:NIB_NAMED(@"CalloutInfoCell") forCellReuseIdentifier:kCalloutInfoCellId];
        [self addSubview:self.tableView];
        self.tableView.backgroundColor = [UIColor greenColor];
        
        self.backgroundColor = [UIColor clearColor];
        
        self.tableView.sd_layout
        .topSpaceToView(self, 6)
        .leftEqualToView(self)
        .rightEqualToView(self)
        .heightIs((self.titles.count)*20);
        
        [self setupAutoHeightWithBottomView:self.tableView bottomMargin:16];
    }
    return self;
}

- (void)setTModel:(TraceModel *)tModel
{
    _tModel = tModel;
    
    [self.tableView reloadData];
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CalloutInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kCalloutInfoCellId];
    cell.titleLabel.text = self.titles[indexPath.row];
    cell.titleLabel.font = SYSTEM_FONT(11);

    if (indexPath.row == 0) {
        cell.contentLabel.text = self.tModel.ownerName;
    } else if (indexPath.row == 1) {
        cell.contentLabel.text = self.tModel.eqNo;
    } else if (indexPath.row == 2) {
        cell.contentLabel.text = self.tModel.positionTime;
    } else if (indexPath.row == 3) {
        cell.contentLabel.text = [Utility directionFromAngle:[self.tModel.dir floatValue]];
    } else if (indexPath.row == 4) {
        cell.contentLabel.text = self.tModel.postType;
    } else if (indexPath.row == 5) {
        cell.contentLabel.text = NSStringFormat(@"%@km/h",self.tModel.speed);
    } else {
        cell.contentLabel.text = self.tModel.cnStatus;
    }
    return cell;
}

- (NSArray *)titles {
    if (!_titles) {
        if (_displayType == DisplayTypeTracing) {
            _titles = @[@"车主：",@"设备号：",@"定位时间：",@"定位方向：",@"定位方式：",@"速度：",@"状态："];
        } else {
            _titles = @[@"车主：",@"设备号：",@"定位时间：",@"定位方向：",@"定位方式：",@"速度："];
        }
    }
    return _titles;
}

#define mark - draw -
- (void)drawRect:(CGRect)rect {
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
}

- (void)drawInContext:(CGContextRef)context {

    UIColor *bgColor;
    if (@available(iOS 13.0, *)) {
        bgColor = UIColor.systemBackgroundColor;
    } else {
        bgColor = UIColor.whiteColor;
    }
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, bgColor.CGColor);
    [self getDrawPath:context];
    CGContextFillPath(context);
}

- (void)getDrawPath:(CGContextRef)context {
    
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}


@end
